package com.itheima.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description 预约设置服务接口实现
 * @Author makun
 * @Date 2023/9/15 15:30
 * @Version 1.0
 */
@Service
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public boolean upload(MultipartFile excelFile) {
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        importParams.setSheetNum(1);
        importParams.setKeyIndex(1);
        List<OrderSetting> orderSettings = null;
        try {
            orderSettings = ExcelImportUtil.importExcel(excelFile.getInputStream(), OrderSetting.class, importParams);
        } catch (Exception e) {
            return false;
        }
        for (OrderSetting orderSetting : orderSettings) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String dateStr = dateFormat.format(orderSetting.getOrderDate());
            String[] split = dateStr.split("-");
            // 更新条件
            QueryWrapper<OrderSetting> orderSettingQueryWrapper = new QueryWrapper<>();
            orderSettingQueryWrapper.apply("YEAR(order_date) = {0}", split[0])
                    .apply("MONTH(order_date) = {0}", split[1])
                    .apply("DAY(order_date) = {0}", split[2]);
            if (baseMapper.update(orderSetting, orderSettingQueryWrapper) < 1) {
                baseMapper.insert(orderSetting);
            }
        }
        return true;
    }

    @Override
    public Result getOrderSettingByMonth(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        String dateStr = dateFormat.format(date);
        String[] split = dateStr.split("-");
        // 查询条件
        QueryWrapper<OrderSetting> orderSettingQueryWrapper = new QueryWrapper<>();
        orderSettingQueryWrapper.apply("YEAR(order_date) = {0}", split[0])
                .apply("MONTH(order_date) = {0}", split[1]);
        // 查询
        List<OrderSetting> orderSettings = baseMapper.selectList(orderSettingQueryWrapper);
        // 封装数据
        List<Map<String, Object>> orderSettingList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Map<String, Object> orderSettingMap = new HashMap<>();
            orderSettingMap.put("date", orderSetting.getOrderDate().getDate());
            orderSettingMap.put("number", orderSetting.getNumber());
            orderSettingMap.put("reservations", orderSetting.getReservations());
            orderSettingList.add(orderSettingMap);
        }


        return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, orderSettingList);
    }

    @Override
    public boolean editNumberByDate(OrderSetting orderSetting) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = dateFormat.format(orderSetting.getOrderDate());
        String[] split = dateStr.split("-");
        // 查询条件
        QueryWrapper<OrderSetting> orderSettingQueryWrapper = new QueryWrapper<>();
        orderSettingQueryWrapper.apply("YEAR(order_date) = {0}", split[0])
                .apply("MONTH(order_date) = {0}", split[1])
                .apply("DAY(order_date) = {0}", split[2]);

        // 更新
        boolean flag = baseMapper.update(orderSetting, orderSettingQueryWrapper) > 0;
        // 添加
        if (!flag) {
            baseMapper.insert(orderSetting);
        }

        return true;
    }

    @Override
    public List<OrderSetting> getOrderSettingByYearMonth(Integer year, Integer month) {
        // 查询条件
        QueryWrapper<OrderSetting> orderSettingQueryWrapper = new QueryWrapper<>();
        orderSettingQueryWrapper.apply("YEAR(order_date) = {0}", year)
                .apply("MONTH(order_date) = {0}", month);
        // 查询
        return baseMapper.selectList(orderSettingQueryWrapper);
    }
}
